﻿var Valid;

$(document).ready(function() {

    // tương tác cho tab profile.
    $(document.body).tabs({
        idtab: '#tab-nav-profile > ul > li > a',
        idtarget: '#tab-nav-profile-target .tab-target'
    });
    $("#changeCaptcha").click(function(){
       $(".img-captcha").attr("src",GLOBAL_ROOT_URL+ "api/captcha.ashx?rand="+Math.random());
    });
    
    $("#btnChangePassword").click(function(evt) {
        var $group = $('#password-popup');
        var isValid = true;
        $group.find(':input[type!=button]').each(function(i, item) {
            if (!$(item).valid()) {
                if (isValid == true)
                    $(item).focus();
                isValid = false;
            }
        });

        if (!isValid)
            evt.preventDefault();
        else {
            $.ajax({
                async: false,
                dataType: 'json',
                url: "/api/user.ashx",
                type: 'POST',
                data: {
                    action: 'savePassword',
                    oldPassword: $("#OldPassword").val(),
                    newPassword: $("#NewPassword").val(),
                    confirmNewPassword: $("#ConfirmNewPassword").val()
                },
                success: function(data) {
                    if (data.result == "1" || data.result == "-1") {
                        window.parent.ChangePasswordComplete(data.result);
                        return;
                    }
                    if (data.result == "0") {
                        jAlert(SystemError);
                        return;
                    }
                    if (data.result == "2") {
                        jAlert(LeakInfoError);
                        return;
                    }
                    if (data.result == "3") {
                        jAlert(OldPassMissMatch);
                        return;
                    }
                    if (data.result == "4") {
                        jAlert(PasswordLengthError);
                        return;
                    }
                    if (data.result == "5") {
                        jAlert(ReTypePassMissMatch);
                    }
                }
            });
        }
    });
    
    $("#btnChangeStatus").click(function(evt) {
        if ($("#CaptchaProfile").val()=="")
        {
            $("#CaptchaProfile").focus();
            evt.preventDefault();
            jAlert("Vui lòng nhập mã xác nhận.")
        }    
        else {
            $.ajax({
                async: false,
                dataType: 'json',
                url: "/api/user.ashx",
                type: 'POST',
                data: {
                    action: 'changeStatus',
                    isHileProfile: $("#HideProfile").attr("checked"),
                    captcha: $("#CaptchaProfile").val()
                },
                success: function(data) {
                    if (data.result == "1" || data.result == "-1") {
                        window.parent.ChangeStatusComplete(data.result);
                        return;
                    }
                    if (data.result == "0") {
                        jAlert(SystemError);
                        return;
                    }
                    if (data.result == "2") {
                        jAlert(EmptyCaptcha);
                        return;
                    }
                    if (data.result == "3") {
                        jAlert(CaptchanotSame);
                        return;
                    }
                }
            });
        }
    });

    //Control Behavior
    $('input').focus(function() {
        $(this).addClass("itemfocus");
    });
    
    $('input').blur(function() {
        $(this).removeClass("itemfocus");
    });

    $("#OldPassword").focus(function() {
        $(this).css("color", "#343434");
    });

    $("#OldPassword").blur(function() {
        $(this).css("color", "#989898");
    });

    $("#NewPassword").focus(function() {
        $(this).css("color", "#343434");
    });

    $("#NewPassword").blur(function() {
        $(this).css("color", "#989898");
    });

    $("#ConfirmNewPassword").focus(function() {
        $(this).css("color", "#343434");
    });

    $("#ConfirmNewPassword").blur(function() {
        $(this).css("color", "#989898");
    });

    //Define validate function
    jQuery.validator.addMethod("checkConfirmNewPassword", function(value, element) {
        if ($("#NewPassword").val() != $("#ConfirmNewPassword").val()) {
            return false;
        }
        return true;
    }, ReTypePassMissMatchError);

    //Define Rule For Form
    Valid = $("#formpassword").validate({
        errorPlacement: function(error, element) {
            $(element).attr("title", $(error).html());
        },
        errorClass: "field-error",
        errorElement: "div",
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
        },
        rules: {
            OldPassword: {
                required: true,
                minlength: 6,
                maxlength: 32
            },
            NewPassword: {
                required: true,
                minlength: 6,
                maxlength: 32
            },
            ConfirmNewPassword: {
                required: true,
                checkConfirmNewPassword: true
            }
        },
        messages: {
            OldPassword: {
                required: OldPasswordMissError,
                minlength: PasswordLengthError,
                maxlength: PasswordLengthError
            },
            NewPassword: {
                required: NewPasswordMissError,
                minlength: PasswordLengthError,
                maxlength: PasswordLengthError
            },
            ConfirmNewPassword: {
                required: ReTypePasswordMissError,
                checkConfirmNewPassword: ReTypePassMissMatchError
            }
        }
    });
    //end: Validate For Form
});